/*
 * BusinessApplicationLike.java
 * business
 *
 * Created by ChenTao on 2017/2/28.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.support.multidex.MultiDex;

import com.baidu.mapapi.SDKInitializer;
import com.microquation.linkedme.android.LinkedME;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.tencent.tinker.loader.app.DefaultApplicationLike;
import com.v5kf.client.lib.V5ClientAgent;
import com.yidiandao.business.utils.BuglyUtils;
import com.yidiandao.business.utils.GsonProvider;
import com.yidiandao.business.utils.OKHttpUtils;
import com.yidiandao.business.utils.PushUtils;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import cn.sharesdk.framework.ShareSDK;

/**
 * Application Delegate
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/13 10:02
 */
public class BusinessApplicationLike extends DefaultApplicationLike {

    public static RefWatcher mRefWatcher;

    /**
     * 全局上下文
     */
    private static Context mContext;

    /**
     * 主线程Handler
     */
    private static Handler mHandler;

    private static int mPid;

    public BusinessApplicationLike(Application application, int tinkerFlags,
            boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime,
            long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime,
                applicationStartMillisTime, tinkerResultIntent);
    }

    public static RefWatcher getRefWatcher() {
        return mRefWatcher;
    }

    public static Context getContext() {
        return mContext;
    }

    public static Handler getHandler() {
        return mHandler;
    }

    public static int getPid() {
        return mPid;
    }

    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);

        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);

        // 安装tinker
        BuglyUtils.installTinker(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // 出错 重启
        // restartWhenError();

        mContext = getApplication();
        // 主线程的Handler
        mHandler = new Handler();
        // 获取主线程的id
        mPid = android.os.Process.myPid();

        GsonProvider.init();

        initLeakCanary();
        initOKHttp();
        initBugly();
        initShareSdk();
        initJPush();
        initUmeng();
        initV5Client();
        initBaiduLocation();
        initLinkedMe();
    }

    private void initOKHttp() {
        OKHttpUtils.init(getApplication());
        OKHttpUtils.setup();
    }

    private void initLinkedMe() {
        if (BuildConfig.DEBUG) {
            //设置debug模式下打印LinkedME日志
            LinkedME.getInstance(getApplication()).setDebug();
        } else {
            LinkedME.getInstance(getApplication());
        }
    }

    private void initBugly() {
        // 注意:必须先配置升级策略否则自定义UI不生效
        BuglyUtils.initUpdateStrategy();
        BuglyUtils.initBugly(getApplication());
        BuglyUtils.setIsDevelopmentDevice(UIUtils.getContext());
    }

    private void initLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(getApplication())) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        mRefWatcher = LeakCanary.install(getApplication());
    }

    private void initBaiduLocation() {
        // 初始化定位sdk，建议在Application中创建
        SDKInitializer.initialize(getApplication());
        //        BaiDuSdkUtils.getInstance().initConfig();
    }

    private void initV5Client() {
        V5ClientAgent.init(getApplication(), null);
    }

    private void initUmeng() {
        UmengUtils.init(getApplication());
    }

    private void initShareSdk() {
        ShareSDK.initSDK(getApplication());
    }

    private void initJPush() {
        PushUtils.init(getApplication());
    }

    //    private void restartWhenError() {
    //        if (BuildConfig.BUSINESS_RELEASE) {
    //            BusinessExceptionHandler catchException = new BusinessExceptionHandler();
    //            Thread.setDefaultUncaughtExceptionHandler(catchException);
    //        }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallback(
            Application.ActivityLifecycleCallbacks callbacks) {
        getApplication().registerActivityLifecycleCallbacks(callbacks);
    }
}
